package com.hitqz.robot.driver.zmq.protocol.handler;

import com.example.tutorial.AddressBookProtos;
import com.hitqz.robot.driver.util.WheelCacheUtil;
import com.hitqz.robot.driver.util.WheelToolkit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 头部控状态处理类
 *
 * @author wengyx
 * @date 2021-09-14 22:34
 **/
@Slf4j
@Component
public class AirQualityHandler implements HandlerZMQService {

    @Override
    public void handlerMsg(String robotSn, byte[] data) throws Exception {
        AddressBookProtos.AirQuality airQuality = AddressBookProtos.AirQuality.parseFrom(data);
        String type = airQuality.getDescriptorForType().getName();
        int ackId = airQuality.getHeader().getAck();
        int objId = airQuality.getHeader().getObj();
        int ret = airQuality.getRet();
        int factoryId = airQuality.getFactoryId();
        List<Integer> itemValList = airQuality.getItemValList();
        if (objId == AddressBookProtos.AirQuality.OBJ_ID.NUL_AIR_QUALITY_NTF_VALUE) {
            this.parseAirData(robotSn, factoryId, itemValList);
        }
        //释放锁
        WheelToolkit.release(ackId,type,objId,true);
    }


    private void parseAirData(String robotSn,Integer factoryId,List<Integer> itemValList){
        HashMap<String, Double> airMap = new HashMap<>();
        switch (factoryId){
            default->{
                //中航油中煤履带
                airMap.put("smoke", (double) itemValList.get(0)/1000);
                airMap.put("temperature", (double) itemValList.get(1)/1000);
                airMap.put("humidity", (double) itemValList.get(2)/1000);
                airMap.put("ch4", (double) itemValList.get(3)/1000);
                airMap.put("CO", (double) itemValList.get(4)/1000);
                airMap.put("O2", (double) itemValList.get(5)/1000);
                airMap.put("CO2", (double) itemValList.get(6)/1000);
                airMap.put("aviation_kerosene", (double) itemValList.get(7)/1000);
                airMap.put("PM2.5", (double) itemValList.get(8)/1000);
                airMap.put("TVOC", (double) itemValList.get(9)/1000);
                airMap.put("formaldehyde", (double) itemValList.get(10)/1000);
                airMap.put("alcohol", (double) itemValList.get(11)/1000);
                airMap.put("H2S", (double) itemValList.get(12)/1000);
                airMap.put("NH3", (double) itemValList.get(13)/1000);
                airMap.put("noise", (double) itemValList.get(14)/1000);
                airMap.put("PM10", (double) itemValList.get(15)/1000);
            }
        }
        //保存信息
        WheelCacheUtil.put(WheelCacheUtil.KEY_AIR_QUALITY,airMap);
    }


}
